<?php
/* This file is part of Mirasol CMS
   (C) 2011 by Chris Alban Hansen.
   Released under the terms of the GNU General Public License.
   See COPYING in the top level directory of the Mirasol CMS installation. */

include "{$_SERVER['DOCUMENT_ROOT']}/includes/config.php";
include "{$_SERVER['DOCUMENT_ROOT']}/includes/settings.php";

if ($loadsubpage == 1)
{
  $dsid = isset ($_GET['dsid']) ? mysql_real_escape_string ($_GET['dsid']) : "";

  /* Get site name */
  $sitename = htmlentities (getsetting ($connection, "sitename"), ENT_COMPAT, "UTF-8", false);
  $domainname = htmlentities (getsetting ($connection, "domainname"), ENT_COMPAT, "UTF-8", false);
  $protocol = getsetting ($connection, "protocol");
?>
<h1>
  <?=$sitename?>
  <a href="<?=$protocol?>://<?=$domainname?>/" title="Go to website" rel="external"><img src="./images/link.gif" alt="<?=$sitename?>" class="externallink" /></a>
</h1>

<div id="datasheets">
  <h3>Datasheets</h3>
  <table cellpadding="0" cellspacing="0" style="width: 100%">
<?php
/* Print the list of templates */
$result = mysql_query ("SELECT id, name FROM ".db_maketablename ($table_datasheets_def)." ORDER BY name");
if (mysql_num_rows ($result) > 0)
  {
    while ($row = mysql_fetch_array ($result))
      {
        $name = htmlentities ($row['name'], ENT_COMPAT, "UTF-8", false);
        print "<tr><td><a href=\"./?p=datasheets&amp;dsid={$row['id']}\"><strong>$name.xml</strong></a></td>";
        print "<td class=\"alignright\"><a href=\"javascript:deldatasheet('$name', {$row['id']});\" title=\"Delete '$name'\"><strong>&times;</strong></a></td></tr>\n";
      }
  }
mysql_free_result ($result);
?>
  </table>

  <div id="addnew1">
    <p>
      <input type="button" value="+" onclick="document.getElementById('addnew2').style.display='block';" class="button" />
      Add new datasheet
    </p>
  </div>

  <div id="addnew2">
    <form action="../includes/adddatasheetdef.php" method="post">
      <table cellpadding="0" cellspacing="0">
        <tr>
          <td><strong>Name:</strong>&nbsp;</td>
          <td><input type="text" name="name" value="type_name_here" maxlength="50" class="textfield" /></td>
        </tr>
        <tr>
          <td colspan="2"><input type="submit" value="Add &raquo;" class="button" /></td>
        </tr>
      </table>
    </form>
  </div>
</div>

<div id="editor">
<?php
/* If no datasheet is selected... */
if (empty ($dsid))
{
?>
  <div id="info">
    <p>&larr; Click on a datasheet to edit it.</p>
  </div>
<?php
} /* if (empty ($dsid)) */
else
{
?>
  <div id="tabs">
    <ul>
      <li><span>Datasheet</span></li>
    </ul>
  </div>

	<form action="../includes/savedatasheetdef.php" method="post">
		<div id="tab1">
<?php
  $result = mysql_query ("SELECT name, fields FROM ".db_maketablename ($table_datasheets_def)." WHERE id='$dsid'");
  if (mysql_num_rows ($result) > 0)
    {
      $row = mysql_fetch_array ($result);
      
      /* Parse fields and xmlfeed */
      print "<label>Name</label><br /><input type=\"text\" name=\"datasheetname\" value=\"".htmlentities ($row['name'], ENT_COMPAT, "UTF-8", false)."\" class=\"textfield\" />\n";
      
      /* Prepare the XML parser and parse the data */
      $xmldata = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".$row['fields'];     
      $xmlres = xml_parser_create ("utf-8");
      xml_parse_into_struct ($xmlres, $xmldata, $values);
      xml_parser_free ($xmlres);
?>
      <table cellpadding="0" cellspacing="0">
        <thead>
          <tr>
            <td class="dscol1"><label>Field name</label></td>
            <td class="dscol2"><label>Label</label></td>
            <td class="dscol3"><label>Default value</label></td>
            <td class="dscol4"><label>Type</label></td>
            <td class="dscol5"><label>Hide</label></td>
            <td class="dscol6">&nbsp;</td>
          </tr>
        </thead>
				
				<tbody>
<?php
      /* Walk through the tags */
      $i = 0;
      $fieldcount = 0;
      while (!empty ($values[$i]))
        {
          if ($values[$i]['level'] == 2 && strcasecmp ($values[$i]['tag'], "field") == 0 && isset ($values[$i]['attributes']['NAME']) && isset ($values[$i]['attributes']['LABEL']) && isset ($values[$i]['attributes']['TYPE']) && isset ($values[$i]['attributes']['DEFVALUE']) && isset ($values[$i]['attributes']['HIDDEN']))
            {
              $fieldcount++;
              print "<tr>";
              print "<td class=\"dscol1\"><input type=\"text\" name=\"name$fieldcount\" value=\"{$values[$i]['attributes']['NAME']}\" class=\"tinytextfield\" /></td>";
              print "<td class=\"dscol2\"><input type=\"text\" name=\"label$fieldcount\" value=\"{$values[$i]['attributes']['LABEL']}\" class=\"tinytextfield\" /></td>";
              print "<td class=\"dscol3\"><input type=\"text\" name=\"defvalue$fieldcount\" value=\"{$values[$i]['attributes']['DEFVALUE']}\" class=\"tinytextfield\" /></td>";
              print "<td class=\"dscol4\"><select name=\"type$fieldcount\">";
              foreach ($fieldtypes_datasheet as $type)
                {
                  $sel = $values[$i]['attributes']['TYPE'] == $type ? " selected=\"selected\"" : "";
                  print "<option value=\"$type\"$sel>$type</option>";
                }
              print "</select></td>";
							$hidden = $values[$i]['attributes']['HIDDEN'] == "1" ? " checked=\"checked\"" : "";
              print "<td class=\"center\"><input type=\"checkbox\" name=\"hidden$fieldcount\" value=\"1\"$hidden /></td>";
              print "<td><input type=\"submit\" name=\"delfield$fieldcount\" value=\"-\" class=\"alignright\" /></td>";
              print "</tr>\n";
            }
          $i++;
        }
?>
				</tbody>
			</table>
			
			<p>
				<input type="submit" name="addfield" value="+" />
				<span class="addfield">Add new field</span>
			</p>
<?php
    }
  mysql_free_result ($result);
?>
		</div>

    <p class="alignright operate">
      &nbsp;<br />
      <input type="hidden" name="fieldcount" value="<?=$fieldcount ?>" />
			<input type="hidden" name="dsid" value="<?=$dsid ?>" />
			<input type="submit" value="Save" class="button" />
		</p>
	</form>
<?php
} /* if (empty ($dsid)) */
?>
</div>

<div class="clear"></div>
<?php
}
?>
